package com.example.communitypro.controller;

import com.example.communitypro.mapper.CommentMapper;
import com.example.communitypro.pojo.Comment;
import com.example.communitypro.pojo.CommunityInformation;
import com.example.communitypro.pojo.Notification;
import com.example.communitypro.pojo.Post;
import com.example.communitypro.service.*;
import com.example.communitypro.util.DateUtil;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostService postService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserCollectionService userCollectionService;
    @Autowired
    private UserLikesService userLikesService;
    @Autowired
    private UserReportService userReportService;
    @Autowired
    private HomeMessageService homeMessageService;
    @Autowired
    private NotificationService notificationService;


    @ResponseBody
    @RequestMapping(value = "/addPost")
    @CrossOrigin
    public Result addPost(Post record){
        System.out.println(record.getPostTime());
        homeMessageService.add(record);
        Result res = postService.addPost(record);



        Notification notification1 = new Notification();
        notification1.setNotificationDate(new Date());
        notification1.setNotificationType(6);
        notification1.setPostNo(record.getCommunityNo());
        notification1.setUserNo(record.getUserNo());
        notificationService.addNotificationPro(notification1,record.getPostTitle());

        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/dropPost")
    @CrossOrigin
    public Result dropPost(Integer postNo){

        commentService.dropPostComment(postNo);
        userLikesService.dropPostLike(postNo);
        userCollectionService.dropAllTypeCollection(postNo,1);
        userReportService.dropTypeReport(postNo,1);
        return postService.dropPost(postNo);
    }

    @ResponseBody
    @RequestMapping(value = "/modifyPost")
    @CrossOrigin
    public Result modifyPost(Post record){
        return postService.modifyPost(record);
    }

    @ResponseBody
    @RequestMapping(value = "/queryPost")
    @CrossOrigin
    public Result queryPost(Integer postNo){
        return postService.queryPost(postNo);
    }

    @ResponseBody
    @RequestMapping(value = "/queryPostAndUser")
    @CrossOrigin
    public Result queryPostAndUser(Integer postNo,Integer user_no){
        return postService.queryPostAndUser(postNo,user_no);
    }

    @ResponseBody
    @RequestMapping(value = "/queryHotCommunityPost")
    @CrossOrigin
    public Result queryHotCommunityPost(Integer communityNo){
        return postService.queryHotCommunityPost(communityNo);
    }

    @ResponseBody
    @RequestMapping(value = "/queryCommunityPost")
    @CrossOrigin
    public Result queryCommunityPost(Integer communityNo,Integer postStatus){

        if (-100 == communityNo) {
            return postService.queryAllPost();
        }
        if (-200 == postStatus){
            postStatus = -1;
        }

        return postService.queryCommunityPassPost(communityNo, postStatus);
    }

    @ResponseBody
    @RequestMapping(value = "/queryCommunityPassPost")
    @CrossOrigin
    public Result queryCommunityPassPost(Integer  communityNo,Integer postStatus){
        if (-100 == communityNo){
            communityNo = -1;
        }
        return postService.queryCommunityPassPost(communityNo,postStatus);
    }

    @ResponseBody
    @RequestMapping(value = "/queryUserPost")
    @CrossOrigin
    public Result queryUserPost(Integer userNo){
        return postService.queryUserPost(userNo);
    }

    @ResponseBody
    @RequestMapping(value = "/queryPassPost")
    @CrossOrigin
    public Result queryPassPost(Integer user_no,Integer post_status){
        return postService.queryPassPost(user_no,post_status);
    }

    @ResponseBody
    @RequestMapping(value = "/queryAllPost")
    @CrossOrigin
    public Result queryAllPost(){
        return postService.queryAllPost();
    }

    @ResponseBody
    @RequestMapping(value = "/queryAllPostPro")
    @CrossOrigin
    public Result queryAllPostPro(){
        return postService.findAllPost();
    }

    @ResponseBody
    @RequestMapping(value = "/queryHotPost")
    @CrossOrigin
    public Result queryHotPost(){
        return postService.queryHotPost();
    }

    @ResponseBody
    @RequestMapping(value = "/queryAllPass")
    @CrossOrigin
    public Result queryAllPass(Integer post_status){
        return postService.queryAllPass(post_status);
    }

    @ResponseBody
    @RequestMapping(value = "/find")
    @CrossOrigin
    public Result find(Integer user_no){
        return postService.find(user_no);
    }

    @ResponseBody
    @RequestMapping(value = "/recommend")
    @CrossOrigin
    public Result recommend(Integer user_no){
        return postService.recommend(user_no);
    }

    @ResponseBody
    @RequestMapping(value = "/like")
    @CrossOrigin
    public Result like(Integer user_no){
        return postService.like(user_no);
    }


    @ResponseBody
    @RequestMapping(value = "/countPost")
    @CrossOrigin
    public List<Integer> countPost(){
        Date date = new Date();

        List<Integer> list = new ArrayList<>();
        list.add(postService.countByDate(DateUtil.getTimeByTransformation(date, "yyyy-MM-dd")));

        for (int i = 1; i <= 6; i++) {
            list.add(postService.countByDate(DateUtil.getTimeByTransformation(new Date(date.getTime()-24*3600*1000*i), "yyyy-MM-dd")));
        }
//        list.add(postService.countByDate(DateUtil.getTimeByTransformation(new Date(date.getTime()-24*3600*1000*2), "yyyy-MM-dd")));
//        list.add(postService.countByDate(DateUtil.getTimeByTransformation(new Date(date.getTime()-24*3600*1000*3), "yyyy-MM-dd")));
//        list.add(postService.countByDate(DateUtil.getTimeByTransformation(new Date(date.getTime()-24*3600*1000*4), "yyyy-MM-dd")));
//        list.add(postService.countByDate(DateUtil.getTimeByTransformation(new Date(date.getTime()-24*3600*1000*5), "yyyy-MM-dd")));
//        list.add(postService.countByDate(DateUtil.getTimeByTransformation(new Date(date.getTime()-24*3600*1000*6), "yyyy-MM-dd")));
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/selectPostByKey")
    @CrossOrigin
    public Result selectPostByKey(String keyword){
        return postService.selectPost(keyword);
    }
}
